package com.yunpuvip.sachet.biz.service.impl;

import com.yunpuvip.sachet.biz.entity.Payment;
import com.yunpuvip.sachet.biz.mapper.PaymentMapper;
import com.yunpuvip.sachet.biz.model.params.PaymentParam;
import com.yunpuvip.sachet.biz.model.result.PaymentResult;
import  com.yunpuvip.sachet.biz.service.PaymentService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunpuvip.sachet.modular.page.LayuiPageFactory;
import com.yunpuvip.sachet.modular.page.LayuiPageInfo;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 支付账单 服务实现类
 * </p>
 *
 * @author wushaohong
 * @since 2019-08-10
 */
@Service
public class PaymentServiceImpl extends ServiceImpl<PaymentMapper, Payment> implements PaymentService {

    @Override
    public void add(PaymentParam param){
        Payment entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(PaymentParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(PaymentParam param){
        Payment oldEntity = getOldEntity(param);
        Payment newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public PaymentResult findBySpec(PaymentParam param){
        return null;
    }

    @Override
    public List<PaymentResult> findListBySpec(PaymentParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(PaymentParam param){
        Page pageContext = getPageContext();
        QueryWrapper<Payment> objectQueryWrapper = new QueryWrapper<>();
        IPage page = this.page(pageContext, objectQueryWrapper);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(PaymentParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Payment getOldEntity(PaymentParam param) {
        return this.getById(getKey(param));
    }

    private Payment getEntity(PaymentParam param) {
        Payment entity = new Payment();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
